package com.zhongyu.controller;

import com.zhongyu.config.MD5Utils;
import com.zhongyu.pojo.Role;
import com.zhongyu.pojo.User;
import com.zhongyu.service.PermissionService;
import com.zhongyu.service.UserService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import com.zhongyu.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
/*
员工个人信息
 */
@RestController
@RequestMapping("/sys/user/")
public class UsermimaController {
    @Resource
    private PermissionService permissionService;
    @Resource
    private UserService userService;
    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize,UserVo userVo){
        PageResult pageResult=userService.searchAll(pageNo,pageSize,userVo);
        return ResultVo.success(pageResult);

    }
    @GetMapping("/list")
    public ResultVo selectById(HttpSession session){
        User loginUser= (User) session.getAttribute("loginUser");
        Integer id=loginUser.getId();
        List<User> list= userService.searchById(id);
        User user =list.get(0);
        return ResultVo.success(user);
    }
    @GetMapping("/grant")
    public ResultVo grant1(@RequestParam Integer userId ,Integer roleId){
        try {
            userService.grant(userId, roleId);
            return ResultVo.success("员工授权成功！");
        }catch (RuntimeException ex){
            return ResultVo.failure("员工授权失败！",ex);
        }
    }
    @PostMapping("/update")
    public ResultVo update(@RequestBody User user, HttpSession session) {
        try {
            user.setModifyDate(LocalDateTime.now());
            User loginUser= (User) session.getAttribute("loginUser");
            if (loginUser!=null){
                user.setCreatedBy(loginUser.getId());
            }
            userService.update(user);
            return ResultVo.success("修改员工成功!");
        }catch (Exception ex){

            return ResultVo.failure("员工修改失败!",ex);
        }
    }
    @PostMapping("/updatepwd")
    public ResultVo updatepwd(@RequestBody User user, HttpSession session) {
        try {
            User loginUser= (User) session.getAttribute("loginUser");
                String salt = "qwert";
                String password = MD5Utils.md5(salt + user.getPassword());
                user.setPassword(password);
                user.setSalt(salt);
                userService.update(user);
                return ResultVo.success("修改员工密码成功!");
        }catch (Exception ex) {
            return ResultVo.failure("员工密码修改失败!", ex);
        }
    }
    @GetMapping("/search4Tree")
    public ResultVo search4Tree1(Integer id){
        // List<TreeNode> list=permissionService.search4Tree1(id);
        //return ResultVo.success(list);
        List<Role> list= permissionService.searchAll(null);
        return  ResultVo.success(list);
    }
   /* @PostMapping("/updatepwd")
    public ResultVo updatepwd(@RequestBody User user, HttpSession session) {
        try {
            String salt = "qwert";
            String password = MD5Utils.md5(salt+user.getPassword());
            user.setPassword(password);
            user.setSalt(salt);
            userService.update(user);
            return ResultVo.success("修改用户密码成功!");
        }catch (Exception ex) {
            return ResultVo.failure("用户密码修改失败!", ex);
        }
    }*/
}
